package cn.zd.demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Set;

import cn.zd.demo1.webview.MyChromeWebClient;
import cn.zd.demo1.webview.MyWebViewClient;

public class WebViewActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener {

    private WebView webView;
    private ProgressBar pb;
    private TextView tv_title;
    private TextView tv_res;
    private ImageView iv;
    private Spinner sp_schema;
    private Callback callback;
    private String[] schemas = new String[]{"http://","https://"};
    private String schema;
    private EditText et_uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        findViewById(R.id.tv_1).setOnClickListener(this);
        findViewById(R.id.tv_2).setOnClickListener(this);
        findViewById(R.id.tv_3).setOnClickListener(this);
        findViewById(R.id.tv_4).setOnClickListener(this);
        findViewById(R.id.tv_js).setOnClickListener(this);
        findViewById(R.id.tv_js1).setOnClickListener(this);
        et_uri = findViewById(R.id.et_uri);
        findViewById(R.id.btn_go).setOnClickListener(this);
        iv = findViewById(R.id.iv);
        tv_title = findViewById(R.id.tv_title);
        tv_res = findViewById(R.id.tv_jsback);
        pb = findViewById(R.id.pb);
        webView = findViewById(R.id.wv_1);
        sp_schema = findViewById(R.id.sp_schema);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, R.layout.spinner_itemview, schemas);
        sp_schema.setAdapter(arrayAdapter);
        sp_schema.setSelection(0);
        sp_schema.setOnItemSelectedListener(this);
        callback = new Callback() {
            @Override
            public void setTitle(String title) {
                Toast.makeText(WebViewActivity.this, "标题是" + title, Toast.LENGTH_SHORT).show();
                tv_title.setText(title);
            }

            @Override
            public void onProgressChanged(int newProgress) {
                pb.setProgress(newProgress);
            }

            @Override
            public void onPageStarted(String url, Bitmap favicon) {
                iv.setImageBitmap(favicon);
            }

            @Override
            public void onPageFinished(String url) {
                Toast.makeText(WebViewActivity.this,"页面加载完成",Toast.LENGTH_SHORT);
            }

            @Override
            public void shouldOverrideUrlLoading(String query) {
                tv_res.setText("js调Android自定义schema:"+query);
            }
        };
    }

    @Override
    public void onClick(View v) {
        webView.setWebViewClient(new MyWebViewClient(this,callback));
        webView.getSettings().setJavaScriptEnabled(true);
        webView.addJavascriptInterface(this,"androidJsCallback");
        if(v.getId() == R.id.tv_1) {
            webView.loadUrl("file:///android_asset/local.html");
        } else if(v.getId() == R.id.tv_2) {
            // 外部打开
            webView.setWebViewClient(null);
            // 设置js支持
            webView.loadUrl("https://baidu.com");
        } else if(v.getId() == R.id.tv_3) {
            // 内部打开
            webView.loadUrl("https://m.baidu.com");
        } else if(v.getId() == R.id.tv_4) {
            // 内部打开
            webView.setWebChromeClient(new MyChromeWebClient(this,callback));
            webView.loadUrl("https://m.baidu.com");
        } else if (v.getId() == R.id.tv_js) {
            webView.evaluateJavascript("javascript:alert('hello,android')",null);
        } else if (v.getId() == R.id.tv_js1) {
            webView.evaluateJavascript("javascript:myJs('hello,android')", new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {
                    Toast.makeText(WebViewActivity.this,"页面js执行结果："+value,Toast.LENGTH_SHORT);
                }
            });
        } else if(R.id.btn_go == v.getId()){
            if(TextUtils.isEmpty(et_uri.getText())) {
                Toast.makeText(this,"请输入要访问的网址",Toast.LENGTH_LONG).show();
                return;
            } else if (TextUtils.isEmpty(schema)) {
                Toast.makeText(this,"请选择协议类型",Toast.LENGTH_LONG).show();
                return;
            }
            String url = schema + et_uri.getText();
            Intent intent = new Intent(this, CommonWebViewActivity.class);
            Bundle options = new Bundle();
            intent.putExtra("url",url);
            startActivity(intent,options);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    // js 回调
    @JavascriptInterface
    public void hello(String name) {
        tv_res.setText("js回调hello,参数："+name);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        schema = schemas[position];
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        schema = null;
    }

    public interface Callback{
        void setTitle(String title);

        void onProgressChanged(int newProgress);

        void onPageStarted(String url, Bitmap favicon);

        void onPageFinished(String url);

        void shouldOverrideUrlLoading(String query);
    }
}